﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsApp1.utils
{
    public class ConfigCheck
    {
        public List<string> GetIP4()
        {
            List<string> ipList = new List<string>();
            Process cmd = new Process();
            cmd.StartInfo.FileName = "ipconfig.exe";//设置程序名   
            //cmd.StartInfo.Arguments = "/all";  //参数   
                                               //重定向标准输出   
            cmd.StartInfo.RedirectStandardOutput = true;
            cmd.StartInfo.RedirectStandardInput = true;
            cmd.StartInfo.UseShellExecute = false;
            cmd.StartInfo.CreateNoWindow = true;
           
            cmd.Start();

            string info = cmd.StandardOutput.ReadToEnd();

            string x = MidStrEx(info, "以太网适配器 VMware Network Adapter VMnet1:", "子网掩码");
            string y = MidStrEx(info, "连接特定的 DNS 后缀 . . . . . . . : cptgroup.cn", "子网掩码");
            string localIp = MidStrEx(x.Trim() + "flag", "IPv4 地址 . . . . . . . . . . . . :", "flag").Trim();
            string netIp = MidStrEx(y.Trim() + "flag", "IPv4 地址 . . . . . . . . . . . . :", "flag").Trim();
            ipList.Add(localIp);
            ipList.Add(netIp);
            cmd.WaitForExit();
            cmd.Close();
            return ipList;
        }


        public static string MidStrEx(string sourse, string startstr, string endstr)
        {
            string result = string.Empty;
            int startindex, endindex;
            int len = endstr.Length;
            try
            {
                startindex = sourse.IndexOf(startstr);
                if (startindex == -1)
                    return result;
                string tmpstr = sourse.Substring(startindex + startstr.Length);
                endindex = tmpstr.IndexOf(endstr);
                if (endindex == -1)
                    return result;
                result = tmpstr.Remove(endindex);
            }
            catch (System.Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("MidStrEx Err:" + ex.Message);
            }
            return result;
        }

    }
}
